package com.easybacklog.client.webservices.subversion;

import com.easybacklog.client.EasyBacklogGlobalVariables;
import com.easybacklog.client.model.ProjectFeature;
import com.easybacklog.client.webservices.ConvertJsonp;
import com.easybacklog.client.webservices.ConvertJsonpToFeatureFile;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class RequestSubversionFileContent {

	private static String url = "http://livesourceagile.com:8080/LiveSourceWebServices/GetSubversionClassDocumentService";

	public static void getJsonpRequest(String projectURL, final String filePath) {

		// String username = EncryptText.decrypt(GetCookies.getUsername(project
		// .getProjectID()));
		String username = null;

		// String password =
		// EncryptText.decrypt(GetCookies.getPassword(project.getProjectID()));
		String password = null;

		JsonpRequestBuilder jsonp = new JsonpRequestBuilder();

		jsonp.setTimeout(15000);

		String urlSave = url + "?repositoryURL=" + projectURL + "&filePath="
				+ filePath + "&username=" + username + "&password=" + password;

		jsonp.requestObject(urlSave, new AsyncCallback<JavaScriptObject>() {

			public void onFailure(Throwable throwable) {

				System.out.println("Error: " + throwable);
			}

			public void onSuccess(JavaScriptObject feed) {

				JSONObject entry = new JSONObject(feed);

				ProjectFeature feature = EasyBacklogGlobalVariables.projectFeatures
						.get(filePath);

				feature.setFileContent(ConvertJsonp.convertToString(entry
						.get("fileContent")));

				feature = ConvertJsonpToFeatureFile.convert(feature, entry);

				EasyBacklogGlobalVariables.projectFeatures.put(filePath,
						feature);
			}
		});
	}
}
